package com.ssm.controller;

import com.ssm.po.Admin;
import com.ssm.po.Clas;
import com.ssm.po.Student;
import com.ssm.service.clasService;
import com.ssm.service.instituteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private clasService clasService;

    @Autowired
    private instituteService instituteService;
    @RequestMapping("showSearch")
    public String showSearch(HttpServletRequest request,Model model){
        Admin admin = (Admin) request.getSession(true).getAttribute("admin");
        if (admin != null){
            request.getSession(true).setAttribute("i_show",instituteService.show());
            request.getSession(true).setAttribute("c_show",clasService.show());
            return "clas_searchandmanage";
        }else {
            model.addAttribute("msg","登录信息已失效，请重新登录");
            return "index";
        }
    }

    @RequestMapping("/doSearch")
    public String doSearch(Clas clas, Model model){
        List<Clas> classes = clasService.doSearch(clas);
        model.addAttribute("classes",classes);
        return "forward:/class/showSearch";
    }

    @RequestMapping("doIns")
    public String doIns(Clas clas,Model model){
        clasService.doIns(clas);
        return "redirect:/class/doSearch";
    }

    @RequestMapping("doUpdate")
    public String doUpdate(Clas clas){
        clasService.doUpdate(clas);
        return "redirect:/class/doSearch";
    }

    @RequestMapping("doDel")
    public String doDel(Integer[] ids){
        for (Integer id : ids) {
            clasService.doDel(id);
        }
        return "redirect:/class/doSearch";
    }
}
